package com.uidt.project.ptsj.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.uidt.common.utils.DateUtils;
import com.uidt.common.utils.SecurityUtils;
import com.uidt.common.utils.StringUtils;
import com.uidt.framework.security.LoginUser;
import com.uidt.framework.security.service.SysLoginService;
import com.uidt.project.ptsj.obj.PlaceScoreRankObj;
import com.uidt.project.system.domain.SysUser;
import com.uidt.project.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uidt.project.ptsj.mapper.PlaceInfoMapper;
import com.uidt.project.ptsj.domain.PlaceInfo;
import com.uidt.project.ptsj.service.IPlaceInfoService;

/**
 * 场所信息Service业务层处理
 * 
 * @author uidt
 * @date 2024-08-15
 */
@Service
public class PlaceInfoServiceImpl implements IPlaceInfoService 
{
    @Autowired
    private PlaceInfoMapper ptsjPlaceInfoMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询单位信息
     *
     * @param placeCode 单位信息主键
     * @return 单位信息
     */
    @Override
    public PlaceInfo selectPlaceInfoByPlaceCode(String placeCode)
    {
        return ptsjPlaceInfoMapper.selectPlaceInfoByPlaceCode(placeCode);
    }

    /**
     * 查询单位信息列表
     *
     * @param ptsjPlaceInfo 单位信息
     * @return 单位信息
     */
    @Override
    public List<PlaceInfo> selectPlaceInfoList(PlaceInfo ptsjPlaceInfo)
    {
        LoginUser user = SecurityUtils.getLoginUser();
        if(!user.getUser().isAdmin()){
            if(user.getUser().getDept()!=null && StringUtils.isNotEmpty(user.getUser().getDept().getDeptcode())){
                ptsjPlaceInfo.setGxdwdm(sysDeptService.getDeptAuthByCodeForPlaceAndHouse(user.getUser().getDept().getDeptcode()));
            }else{
                return new ArrayList<PlaceInfo>();
            }
        }
        return ptsjPlaceInfoMapper.selectPlaceInfoList(ptsjPlaceInfo);
    }

    /**
     * 新增单位信息
     *
     * @param ptsjPlaceInfo 单位信息
     * @return 结果
     */
    @Override
    public int insertPlaceInfo(PlaceInfo ptsjPlaceInfo)
    {
        return ptsjPlaceInfoMapper.insertPlaceInfo(ptsjPlaceInfo);
    }

    /**
     * 修改单位信息
     *
     * @param ptsjPlaceInfo 单位信息
     * @return 结果
     */
    @Override
    public int updatePlaceInfo(PlaceInfo ptsjPlaceInfo)
    {
        ptsjPlaceInfo.setUpdateTime(DateUtils.getNowDate());
        return ptsjPlaceInfoMapper.updatePlaceInfo(ptsjPlaceInfo);
    }

    /**
     * 批量删除单位信息
     *
     * @param placeCodes 需要删除的单位信息主键
     * @return 结果
     */
    @Override
    public int deletePlaceInfoByPlaceCodes(String[] placeCodes)
    {
        return ptsjPlaceInfoMapper.deletePlaceInfoByPlaceCodes(placeCodes);
    }

    /**
     * 删除单位信息信息
     *
     * @param placeCode 单位信息主键
     * @return 结果
     */
    @Override
    public int deletePlaceInfoByPlaceCode(String placeCode)
    {
        return ptsjPlaceInfoMapper.deletePlaceInfoByPlaceCode(placeCode);
    }

    /**
     * 获取当前场所的积分排名
     *
     * @param placeCode 单位信息主键
     * @return 结果
     */
    @Override
    public PlaceInfo getPlaceRankByCode(String placeCode)
    {

        PlaceInfo placeInfo = ptsjPlaceInfoMapper.selectPlaceInfoByPlaceCode(placeCode);

        // 获取所有分数的排序以及每个分数的场所数量
        List<PlaceScoreRankObj> list = ptsjPlaceInfoMapper.getPlaceScoreRank();

        int rank=1;
        for(PlaceScoreRankObj obj:list){
            if(obj.getScore().equals(placeInfo.getScore().toString())){
                placeInfo.setRank(rank);
                break;
            }else {
                rank+=Integer.parseInt(obj.getCount());
            }
        }

        return placeInfo;
    }

    /**
     * 获取所有场所的积分排名，支持分页
     *
     * @param ptsjPlaceInfo 单位信息主键
     * @return 结果
     */
    @Override
    public List<PlaceInfo> getPlaceRankList(PlaceInfo ptsjPlaceInfo)
    {

        List<PlaceInfo> placeInfoList = ptsjPlaceInfoMapper.selectRankList(ptsjPlaceInfo);

        // 获取所有分数的排序以及每个分数的场所数量
        List<PlaceScoreRankObj> list = ptsjPlaceInfoMapper.getPlaceScoreRank();

        for(PlaceInfo placeInfo:placeInfoList){
            int rank=1;
            for(PlaceScoreRankObj obj:list){
                if(obj.getScore().equals(placeInfo.getScore().toString())){
                    placeInfo.setRank(rank);
                    break;
                }else {
                    rank+=Integer.parseInt(obj.getCount());
                }
            }
        }


        return placeInfoList;
    }
}
